package com.aijle.videopaper.service;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.service.wallpaper.WallpaperService;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
import android.widget.LinearLayout;
import android.view.WindowManager.LayoutParams;

import com.aijle.videopaper.util.OverlayHelper;
import com.aijle.videopaper.util.SimpleWidgetManager;

public class VideoWallpaperService extends WallpaperService {
    private final String    TAG         = getClass().getSimpleName();
    final static int        pixFormat   = PixelFormat.RGBA_8888;
    protected WindowManager windowManager;
    protected WidgetGroup   widgetGroup;
    protected SurfaceHolder surfaceHolder;
    OverlayHelper mOverlayHelper;

    @Override
    public Engine onCreateEngine() {
        return new VideoEngine();
    }

    public class WidgetGroup extends ViewGroup
    {

        private final String    TAG = getClass().getSimpleName();

        public WidgetGroup( Context context )
        {
            super( context );
            setWillNotDraw( true );
        }

        @Override
        protected void onLayout( boolean changed, int l, int t, int r, int b )
        {
            layout( l, t, r, b );
        }

    }
    private class VideoEngine extends Engine implements Callback {
        View view;

        @Override
        public void onCreate(SurfaceHolder holder) {
            super.onCreate(holder);
            surfaceHolder = holder;
            surfaceHolder.addCallback( this );
            widgetGroup = new WidgetGroup( getApplicationContext() );
            widgetGroup.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
            widgetGroup.setAddStatesFromChildren( true );
            holder.setFormat( pixFormat );

            SimpleWidgetManager widgetManager = new SimpleWidgetManager(getApplicationContext(), widgetGroup);
            mOverlayHelper = new OverlayHelper(getApplicationContext(), widgetManager);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            if (visible) {
                view = mOverlayHelper.reload();
            } else {
                mOverlayHelper.close();
                view = null;
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            synchronized( surfaceHolder )
            {
                Canvas canvas = surfaceHolder.lockCanvas();
                widgetGroup.layout( 0, 0, width, height );
                if (view != null)
                    view.layout( 0, 0, width, height );
                widgetGroup.draw( canvas );
                surfaceHolder.unlockCanvasAndPost( canvas );
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    }
}
